package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class IndexController {


    @Autowired
    CategoryService categoryService;

    @Autowired
    RedissonClient redisson;


    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @GetMapping({"/","/index.html"})
    public String indexPage(Model model){

        //TODO:1、查出所有一级分类

       List<CategoryEntity> categoryEntities =  categoryService.getLevel1Categorys();

       model.addAttribute("categorys",categoryEntities);

        return "success";
    }

    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String,List<Catelog2Vo>> getCatelogJson(){

        Map<String,List<Catelog2Vo>> catelogJson = categoryService.getCatelogJson();

        return catelogJson;
    }

    @ResponseBody
    @GetMapping("/hello")
    public String hello(){

        RLock lock = redisson.getLock("my-lock");

        lock.lock();

        try{

            System.out.println("加锁成功"+Thread.currentThread().getId());

            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {

            System.out.println("释放锁....");
            lock.unlock();
        }


        return "Hello!";
    }

    @ResponseBody
    @GetMapping("/write")
    public String writeValue(){

        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        RLock rLock = lock.writeLock();

        String s = "";

        try {

            rLock.lock();

            s = UUID.randomUUID().toString();


            Thread.sleep(30000);
            stringRedisTemplate.opsForValue().set("writeValue",s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }


        return s;
    }

    @GetMapping("/read")
    @ResponseBody
    public String readValue(){

        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");

        RLock rLock = lock.readLock();

        rLock.lock();

        String writeValue = null;

        try{

            writeValue = stringRedisTemplate.opsForValue().get("writeValue");



        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }

        return writeValue;

    }

    @ResponseBody
    @GetMapping("/park")
    public String park() throws InterruptedException {

        RSemaphore park = redisson.getSemaphore("park");

        park.acquire();

        return "ok";
    }


    @ResponseBody
    @GetMapping("/go")
    public String go(){

        RSemaphore park = redisson.getSemaphore("park");

        park.release(); //释放一个信号

        return "ok";
    }

    @GetMapping("/lockdoor")
    @ResponseBody
    public String lockDoor() throws InterruptedException {

        RCountDownLatch door = redisson.getCountDownLatch("door");

        door.trySetCount(5);

        door.await(); //等待闭锁都完成


        return  "放假了...";
    }

    @ResponseBody
    @GetMapping("/gogogo/{id}")
    public String gogogo(@PathVariable("id") Long id){

        RCountDownLatch door = redisson.getCountDownLatch("door");
        door.countDown();;


        return id+"班的人都走了..";
    }
}
